package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Reference
    RoleService roleService;

//    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
//    @RequestMapping("/add")
    /**
     * @Author tanxinshi
     * @Date 2020/3/7 15:31
     * @Version v1.0
     * @Description 角色分页查询
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("******************"+queryPageBean);
        PageResult pageResult = roleService.findByPage(queryPageBean);
        log.debug("角色分页查询成功!!");
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, pageResult);
    }
    /**
     * @Author tanxinshi
     * @Date 2020/3/7 15:31
     * @Version v1.0
     * @Description 新增
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        log.debug("***********************************"+map);
        Object orole= map.get("role");
        JSONObject objRole1 = (JSONObject) orole;
        Role role =objRole1.toJavaObject(Role.class);
        Object opermissionIds= map.get("permissionIds");
        JSONArray jpermissionIds = (JSONArray) opermissionIds;
        Integer[] permissionIds = jpermissionIds.toArray(new Integer[]{});
        Object omenuIds= map.get("menuIds");
        JSONArray jmenuIds = (JSONArray) omenuIds;
        Integer[] menuIds = jmenuIds.toArray(new Integer[]{});
        roleService.add(role,permissionIds,menuIds);
        log.debug("添加角色成功");
        return new Result(true,"添加角色成功了");
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds){
        roleService.edit(role,permissionIds);
        log.debug("修改成功");
        return new Result(true,"编辑角色成功");
    }
    /**
     * @Author tanxinshi
     * @Date 2020/3/7 17:12
     * @Version v1.0
     * @Description 添加
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("************************" );
        List<Role> role = roleService.findAll();
        log.debug("查询成功！！！");
        return new Result(true,"角色查询成功",role);
    }
    /**
     * @Author tanxinshi
     * @Date 2020/3/7 17:47
     * @Version v1.0
     * @Description 编辑
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        log.debug("角色查询成功");
        return new Result(true,"角色查询成功",role);
    }
    @RequestMapping("/findPermissionIdsById")
    public Result findPermissionIdsById(Integer id){
        List<Integer> roleIds = roleService.findPermissionIdsById(id);
        return new Result(true,"根据id查询权限成功",roleIds);
    }
    @RequestMapping("/findMenuById")
    public Result findMenuById(Integer id){
        List<Integer> roleIds = roleService.findMenuById(id);
        return new Result(true,"根据id查询权限成功",roleIds);
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
       roleService.delById(id);
        log.debug("角色删除成功!!");
        return new Result(true,"删除角色成功");
    }
}
